import { _list } from '../../common/api/award'
import { formatDate } from '../../utils/util'
const computedBehavior = require('miniprogram-computed')
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    uid: '',
    list: [],
    totalCount: 0,
    finished: false,
    pageIndex: 1,
    pageSize: 10
  },
  computed: {
    showedList() {
      return this.data.list.map(item => {
        item.GrantTime = formatDate(new Date(item.GrantTime), 'yyyy/MM/dd hh:mm')
        return item
      })
    }
  },
  methods: {
    list() {
      app.loading('加载中')
      this.data.uid = wx.getStorageSync('uid')
      _list(
        this.data.uid,
        this.data.pageIndex,
        this.data.pageSize
      ).then(res => {
        wx.hideLoading()
        if (res.data.IsSuccess) {
          let list = res.data.Data.GrantList
          this.data.totalCount = res.data.Data.DataCount
          this.data.finished = list.length >= this.data.totalCount
          this.setData({
            list,
            finished: this.data.finished
          })
        }
      }).catch(err => {
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: '请求失败，请稍后再试',
          showCancel: false
        })
      })
    },
    concatList() {
      _list(
        this.data.uid,
        this.data.pageIndex,
        this.data.pageSize
      ).then(res => {
        this.data.totalCount = res.data.Data.DataCount
        let list = res.data.Data.GrantList
        this.data.list = this.data.list.concat(list)
        this.setData({
          list: this.data.list
        })
      }).catch(err => {
        console.log(err)
        wx.showModal({
          title: '对不起',
          content: '网络错误，请稍后再试',
          showCancel: false
        })
      })
    },
    onLoad(options) { },
    onReady() { },
    onShow() {
      this.list()
    },
    onHide() { },
    onUnload() { },
    onPullDownRefresh() { },
    onReachBottom() {
      if (this.data.list.length >= this.data.totalCount) {
        this.setData({
          finished: true
        })
      } else {
        this.data.pageIndex += 1
        this.concatList()
      }
    },
    onShareAppMessage() {
      return app.shareInfo
    }
  }
})